﻿using System;
using System.Data;
using TraditionalMalteseCuisine.App_Code.Functions;
using TraditionalMalteseCuisine.App_Code.Objects;
using TraditionalMalteseCuisine.App_Code.Operations;

namespace TraditionalMalteseCuisine
{
    public partial class Default : System.Web.UI.Page
    {
        #region Page Load
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindMostRecentRecipes();
                BindMostLikedRecipes();
                BindMostRatedRecipes();
                BindRecommendedMembers();
            }
        }
        #endregion


        #region Events
        protected void cmbMaxRecentRecords_SelectedIndexChanged(object sender, EventArgs e)
        {
            MaxRecentRecords = int.Parse(cmbMaxRecentRecords.SelectedValue.ToString());
            BindMostRecentRecipes();
        }

        protected void cmbMaxLikedRecords_SelectedIndexChanged(object sender, EventArgs e)
        {
            MaxLikedRecords = int.Parse(cmbMaxLikedRecords.SelectedValue.ToString());
            BindMostLikedRecipes();
        }

        protected void cmbMaxRatedRecords_SelectedIndexChanged(object sender, EventArgs e)
        {
            MaxRatedRecords = int.Parse(cmbMaxRatedRecords.SelectedValue.ToString());
            BindMostRatedRecipes();
        }

        protected void cmbMaxFollowedMembers_SelectedIndexChanged(object sender, EventArgs e)
        {
            MaxFollowedMembers = int.Parse(cmbMaxFollowedMembers.SelectedValue.ToString());
            BindRecommendedMembers();
        }
        #endregion


        #region Methods
        private void BindMostRecentRecipes()
        {
            try
            {
                DataTable dtRecipes = ReadOp.GetDataTable(Recipe.GetCommand_GetTopMostRecentRecipes(MaxRecentRecords));

                cmbMaxRecentRecords.SelectedValue = MaxRecentRecords.ToString();
                
                //dtRecipes.DefaultView.Sort = string.Format("{0} ASC", Recipe.POSTED_DATE_TAG); 

                dgvMostRecent.DataSource = dtRecipes;
                dgvMostRecent.DataBind();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        private void BindMostLikedRecipes()
        {
            try
            {
                DataTable dtRecipes = ReadOp.GetDataTable(Recipe.GetCommand_GetTopMostLikedRecipes(MaxLikedRecords));

                cmbMaxLikedRecords.SelectedValue = MaxLikedRecords.ToString();

                //dtRecipes.DefaultView.Sort = string.Format("{0} ASC", RecipeLike.TOTAL_LIKES_TAG); 

                dgvMostLiked.DataSource = dtRecipes;
                dgvMostLiked.DataBind();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        private void BindMostRatedRecipes()
        {
            try
            {
                DataTable dtRecipes = ReadOp.GetDataTable(Recipe.GetCommand_GetTopMostRatedRecipes(MaxRatedRecords));

                cmbMaxRatedRecords.SelectedValue = MaxRatedRecords.ToString();

                //dtRecipes.DefaultView.Sort = string.Format("{0} ASC", RecipeRating.TOTAL_VOTES_TAG); 

                dgvMostRated.DataSource = dtRecipes;
                dgvMostRated.DataBind();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        private void BindRecommendedMembers()
        {
            try
            {
                DataTable dtComments = ReadOp.GetDataTable(Subscription.GetCommand_GetTopSubscribedMembers(MaxFollowedMembers));

                cmbMaxFollowedMembers.SelectedValue = MaxFollowedMembers.ToString();

                //dtComments.DefaultView.Sort = string.Format("{0} ASC", SiteMember.TOTAL_RECIPES_POSTED_TAG); 

                dgvTopMembers.DataSource = dtComments;
                dgvTopMembers.DataBind();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
        #endregion


        #region Properties
        protected int MaxRecentRecords
        {
            get { return ((Session["MaxRecentRecords"] == null) ? 5 : (int)Session["MaxRecentRecords"]); }
            set { Session["MaxRecentRecords"] = value; }
        }

        protected int MaxLikedRecords
        {
            get { return ((Session["MaxLikedRecords"] == null) ? 5 : (int)Session["MaxLikedRecords"]); }
            set { Session["MaxLikedRecords"] = value; }
        }

        protected int MaxRatedRecords
        {
            get { return ((Session["MaxRatedRecords"] == null) ? 5 : (int)Session["MaxRatedRecords"]); }
            set { Session["MaxRatedRecords"] = value; }
        }

        protected int MaxFollowedMembers
        {
            get { return ((Session["MaxFollowedMembers"] == null) ? 5 : (int)Session["MaxFollowedMembers"]); }
            set { Session["MaxFollowedMembers"] = value; }
        }
        #endregion
    }
}